//
//  HZUIViewForGuide.m
//  Constraint
//
//  Created by huazi on 14-5-28.
//  Copyright (c) 2014年 AutoLayoutTestDemo. All rights reserved.
//

#import "HZUIViewForGuide.h"
//#import "AFNetworking.h"
#define DeviceRect1   [UIScreen mainScreen].bounds
#define DeviceHeight1 [UIScreen mainScreen].bounds.size.height
#define DeviceWidth1 [UIScreen mainScreen].bounds.size.width
#define FirstUsedApp [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
@interface HZUIViewForGuide ()<UIScrollViewDelegate>
{
    NSArray *arrayPic;
    int countsPic;
}
@end

@implementation HZUIViewForGuide

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}

- (void)initSubviews:(UIView *)view
{
    self.scrollView =[[UIScrollView alloc] initWithFrame:self.frame];
    [self addSubview:self.scrollView];
    if (self.dataSource!=nil &&[self.dataSource respondsToSelector:@selector(ViewForGuideArrayPics:)])
    {
        
        arrayPic =[self.dataSource ViewForGuideArrayPics:self];
        countsPic =[arrayPic count];
        if ([self.superflag isEqualToString:@"YINDAO"]) {
             self.scrollView.contentSize =CGSizeMake(DeviceWidth1*countsPic, DeviceHeight1);
        }
        else
        {
             self.scrollView.contentSize =CGSizeMake(DeviceWidth1*countsPic, self.frame.size.height);
        }
       
        self.scrollView.pagingEnabled =YES;
        self.scrollView.bounces =NO;
        self.scrollView.delegate = self;
        self.scrollView.showsVerticalScrollIndicator =NO;
        self.scrollView.showsHorizontalScrollIndicator =NO;
        
        if ([self.superflag isEqualToString:@"YINDAO"]) {
            for (int i=0; i<countsPic; i++)
            {
                UIImageView *image =[[UIImageView alloc] initWithFrame:CGRectMake(0+i*DeviceWidth1, 0, DeviceWidth1, DeviceHeight1)];
                image.image =[UIImage imageNamed:[arrayPic objectAtIndex:i]];
                [self.scrollView addSubview:image];
                if (i==countsPic-1)
                {
                    UITapGestureRecognizer *sinletap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapToHideSelf)];
                    sinletap.numberOfTapsRequired =1;
                    image.userInteractionEnabled =YES;
                    [image addGestureRecognizer:sinletap];
                    
                }
                
            }

        }
        else
        {
            for (int i=0; i<countsPic; i++)
            {
                UIImageView *image =[[UIImageView alloc] initWithFrame:CGRectMake(0+i*DeviceWidth1, 0, DeviceWidth1, self.frame.size.height)];
                
                NSString * stringURL = [[arrayPic objectAtIndex:i] objectForKey:@"picurl"];
                NSURL * url = [NSURL URLWithString:stringURL];
                [image sd_setImageWithURL:url];
                
                UITapGestureRecognizer *single = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lzqsinglehandle:)];
                single.numberOfTapsRequired = 1;
                image.tag = 10000+i;
                image.userInteractionEnabled = YES;
                [image addGestureRecognizer:single];
                [self.scrollView addSubview:image];
            }
        }
            
            
    }
    
    if ([self.superflag isEqualToString:@"YINDAO"]) {
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, DeviceHeight1-48, DeviceWidth, 48)];
        [self.pageControl setPageIndicatorTintColor:[UIColor grayColor]];
        [self.pageControl setCurrentPageIndicatorTintColor:TabBarBjSelectColor];
    }
    else
    {
        self.pageControl = [[MyPageControl2 alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 50, DeviceWidth, 48)];
    }
    [_pageControl setNumberOfPages:self.numofpages];
    [_pageControl setCurrentPage:0];
    [self addSubview:_pageControl];
    [view addSubview:self];
   
}
-(void)lzqsinglehandle:(UITapGestureRecognizer *)tap //处理点击链接
{
    [self.dataSource passclickedpicHandle:arrayPic andtag:tap.view.tag];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView1
{
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1
{
    int index = scrollView1.contentOffset.x/DeviceWidth;
    [_pageControl setCurrentPage:index];
}
- (void)TapToHideSelf
{
    NSUserDefaults *sd = [NSUserDefaults standardUserDefaults];
//    [UIView animateWithDuration:0.5
//                     animations:^{
//                         
//                         self.alpha =0.0f;
//                     }
//                     completion:^(BOOL finish)
//     {
//         
//         [self removeFromSuperview];
//         [sd setBool:YES forKey:FirstUsedApp];
//         [sd synchronize];
//         
//         UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"First" bundle:nil];
//         
//         BaseNavigationController *nav = [storyboard instantiateInitialViewController];
//         CDKeyWindow.rootViewController = nav;
//         
//     }];
    
    [sd setBool:YES forKey:FirstUsedApp];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"First" bundle:nil];
    
    BaseNavigationController *nav = [storyboard instantiateInitialViewController];
    CDKeyWindow.rootViewController = nav;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
